package cn.est.serviceImpl;

import cn.est.dto.BrandDto;
import cn.est.dto.ClassifyDto;
import cn.est.dto.Page;
import cn.est.mapper.ClassifyMapper;
import cn.est.pojo.Classify;
import cn.est.pojo.Model;
import cn.est.service.BrandService;
import cn.est.service.ClassifyService;
import cn.est.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ClassifyServiceImpl implements ClassifyService {

    @Autowired
    private ClassifyMapper classifyMapper;

    @Autowired
    private BrandService brandService;

    @Autowired
    private ModelService modelService;

    public Classify getClassifyById(Long id) {
        return classifyMapper.getClassifyById(id);
    }

    public List<Classify> getClassifyListByMap(Map<String, Object> param) {
        return classifyMapper.getClassifyListByMap(param);
    }

    public Integer getClassifyCountByMap(Map<String, Object> param) {
        return classifyMapper.getClassifyCountByMap(param);
    }

    public Integer qdtxAddClassify(Classify classify) {
        classify.setCreatdTime(new Date());
        return classifyMapper.insertClassify(classify);
    }

    public Integer qdtxModifyClassify(Classify classify) {
        classify.setUpdatedTime(new Date());
        return classifyMapper.updateClassify(classify);
    }

    public Integer qdtxDeleteClassifyById(Long id) {
        return classifyMapper.deleteClassifyById(id);
    }

    public Integer qdtxBatchDeleteClassify(String ids) {
        Map<String, List<String>> param = new HashMap<String, List<String>>();
        String[] paramArrays = ids.split(",");
        List<String> idList = new ArrayList<String>();
        for (String temp : paramArrays) {
            idList.add(temp);
        }
        param.put("ids", idList);
        return classifyMapper.batchDeleteClassify(param);
    }

    public Page<Classify> queryClassifyPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) {
        Integer total = classifyMapper.getClassifyCountByMap(param);
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Classify> classifyList = classifyMapper.getClassifyListByMap(param);
        page.setList(classifyList);
        return page;
    }

    @Override
    public List<ClassifyDto> queryclassifytree() {
        List<ClassifyDto> classifyDtoList = null;

        //1、获取所有的分类信息
        classifyDtoList = classifyMapper.selectlist();

        //2、遍历所有的分类信息获取所有的品牌信息
        for(ClassifyDto classifyDto : classifyDtoList){
            if (classifyDto != null && classifyDto.getId() != null){
                Integer cid = classifyDto.getId();
                 List<BrandDto> brandDtoList=brandService.getListByCid(cid);
                    for(BrandDto brandDto:brandDtoList){
                        //3.遍历品牌信息,获取商品信息
                        Integer bid=brandDto.getId();
                        Map map =new HashMap();
                        map.put("classifyId",cid);
                        map.put("brandId",bid);
                        List<Model> modelList= modelService.getModelListByMap(map);
                        brandDto.setModelList(modelList);
                    }
                classifyDto.setBrandDtos(brandDtoList);
            }
        }
        //4、封装数据返回
        return classifyDtoList;
    }
}
